SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_RemoveUser]
	@UserID		int,
	@PortalID   int
AS
	IF @PortalID IS NULL
		BEGIN
			-- Delete SuperUser
			DELETE FROM dbo.DnnProduct_Users
				WHERE  UserId = @UserID
		END
	ELSE
		BEGIN
			-- Remove User from Portal			
			DELETE FROM dbo.DnnProduct_UserPortals
				WHERE  UserId = @UserID
                 AND PortalId = @PortalID
			IF NOT EXISTS (SELECT 1 FROM dbo.DnnProduct_UserPortals WHERE  UserId = @UserID) 
				-- Delete User (but not if SuperUser)
				BEGIN
					DELETE FROM dbo.DnnProduct_Users
						WHERE  UserId = @UserID
							AND IsSuperUser = 0
					DELETE FROM dbo.DnnProduct_UserRoles
						WHERE  UserID = @UserID
				END								
		END
GO
